import { isFunction } from "@/utils/is";
import { EaserTool } from "../tools/EaserTool";
import { PenTool } from "../tools/PenTool";
import { ShapeTool } from "../tools/ShapeTool";
import { TextTool } from "../tools/TextTool";
import { createBaseTool } from "./BaseTool";
import { App } from "leafer-ui";
import { IToolConfig } from "../types";

export const ToolManager = (app:App, config: IToolConfig) => {
  // 工具Map
  let toolMap: Record<string, any> = {
    pen: PenTool,
    text: TextTool,
    shape:ShapeTool,
    easer:EaserTool,
  };
  let toolManager: any;
  let activeTool: string = "";
  // 切换工具
  const setActiveTool = (toolName: string) => {
    if (activeTool === toolName) return;
    toolManager?.unregisterEvents?.();
    activeTool = toolName;
    const tool = toolMap[toolName];
    if(isFunction(tool)){
      toolManager = createBaseTool(app, toolMap[toolName]({app, config,historyUtil}) );
      toolMap[toolName] = toolManager;
    }else {
      toolManager = tool;
    }
    toolManager.registerEvents();
  };
  return {
    setActiveTool,
  };
};
